package cn.linkai.star.graph.core.service.impl;

import cn.linkai.star.graph.comfyui.client.pojo.ComfyuiModel;
import cn.linkai.star.graph.core.service.FreemarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    private final Configuration configuration;

    /**
     * 封装comfyui提交任务的参数
     * @param model 模板数据
     * @return 模板渲染后数据json
     */
    @Override
    public String renderText2Image(ComfyuiModel model) throws IOException, TemplateException {
        //读取模板文件
        Template template = configuration.getTemplate ("t2i.ftlh");

        //渲染模板
        StringWriter writer = new StringWriter ();
        template.process (Map.of ("config", model), writer);
        return writer.toString ();
    }
}
